$(function () {
    var userInfo = LoginStatus();
    useLayUI();
    $(".add-btn").click(function () {
        $(".date-plug").removeClass('date-plug');
        $(".itemselect").removeAttr("lay-filter");
        addJobExper($(this));
        useLayUI();
    })

    //判断是否存在cookie
    if($.cookie("experienceWork")){
        var experienceWork = $.cookie("experienceWork");
      
        var obj = JSON.parse(experienceWork)
        if(obj.length>1){
            for(var i=1;i<obj.length;i++){
                console.log(2)
                $(".add-btn").click();
            }
        }
        //公司
        $("input[name='company[]']").each(function(i,item){
            $(this).val(obj[i].company)
        });
        //时间
        $("input[name='entryTime[]']").each(function(i,item){
            $(this).val(obj[i].entryTime)
        });
        $("input[name='separationTime[]']").each(function(i,item){
            $(this).val(obj[i].separationTime)
        });
        //部门
        $("input[name='department[]']").each(function(i,item){
            $(this).val(obj[i].department)
        });
        //职位
        $("input[name='position[]']").each(function(i,item){
            $(this).val(obj[i].position)
        });
        //描述
        $("textarea[name='workContext[]']").each(function(i,item){
            $(this).val(obj[i].workContext)
        });    

    }
    //提交
    $("#Submitbtn").click(function(){
        var experienceWork = [];
        var companys = [];
        var company = $("input[name='company[]']").each(function(){
            companys.push($(this).val())
        });
        var entryTimes = [];
        var entryTime = $("input[name='entryTime[]']").each(function(){
            entryTimes.push($(this).val())
        });
        var separationTimes = [];
        var separationTime=$("input[name='separationTime[]']").each(function(){
            separationTimes.push($(this).val())
        });
        var departments = [];
        var department=$("input[name='department[]']").each(function(){
            departments.push($(this).val())
        });
        var positions = [];
        var position=$("input[name='position[]']").each(function(){
            positions.push($(this).val())
        });
        var workContexts = [];
        var workContext=$("textarea[name='workContext[]']").each(function(){
            workContexts.push($(this).val())
        });
        for(var i=0;i<companys.length;i++){
            if(companys[i]==""){
                if(entryTimes[i]!=""||departments[i]!=""||positions[i]!=""||separationTimes[i]!=""||workContexts[i]!=""){
                    layerMsg("若存在工作经验公司名称不为空")
                    return false;
                }else{
                    if(i!=0){
                        companys.splice(i,1);
                        entryTimes.splice(i,1);
                        departments.splice(i,1);
                        positions.splice(i,1);
                        separationTimes.splice(i,1);
                        workContexts.splice(i,1);
                    }

                }
            }
            
        }
        for(var i=0;i<companys.length;i++){
            var jsondata = {'company':companys[i],
            'department':departments[i]
            ,"position":positions[i]
            ,"entryTime":entryTimes[i]  //注意时间格式
            ,"separationTime":separationTimes[i]
            ,"workContext":workContexts[i]
            }
            experienceWork.push(jsondata)
        }
        if(companys.length<2 && companys[0]==""){
            layerMsg("请添加信息或跳过这一步")
            return false;
            
        }
        $.cookie('experienceWork',JSON.stringify(experienceWork))
        layerMsg("添加成功")
        setTimeout(function() {
            window.location.href="register-education.html" ;
        },3000);
    })



})